#include "createDB.h"

using namespace std;
using namespace cv;


int DBKeypointAdd(const vector<KeyPoint> img_kp, vector<KeyPoint>& db_kp)
{

	for (int i=0; i<img_kp.size(); i++)
	{
		db_kp.push_back(img_kp[i]);
	}

	return 1;
}

int DBDescriptorAdd(const Mat img_descriptor, vector<Mat>& db_descriptor)
{
	Mat tmp;

	for (int i=0; i<img_descriptor.rows; i++)
	{
		img_descriptor.row(i).copyTo(tmp);    // ATTENTION!!!
		db_descriptor.push_back(tmp);
	}

	return 1;
}

// input: mask_name_arr + training_img_name_arr
// output: mask_arr + training_img_arr
// iterate through the vector of names and read the real images
int getImageList(char dir[FULLNAME_SZ], const vector<string> name_arr, vector<Mat>& image_arr)
{
	Mat img;
	char fullname[FULLNAME_SZ];

	for (int i = 0; i<name_arr.size(); i++)
	{
		sprintf(fullname, "%s/%s",dir, name_arr[i].c_str());

		img = imread(fullname, 1);    // choose '1' to read background image, '0' to read mask image

		image_arr.push_back(img);
	}

	return 1;
}


int createDB(char DBdir[FULLNAME_SZ], char DBfilelist[NAME_SZ], char Testdir[FULLNAME_SZ], char Testfilelist[NAME_SZ],
		 std::vector<cv::KeyPoint>& db_keypoints, cv::Mat& ret_db_descriptors, 
		 std::vector<cv::Mat>& test_db_img)
{

	int sample_sz=200;

	// CREATE FEATURE DATABASE
	/*vector<KeyPoint> db_keypoints;*/
	vector<Mat> db_descriptors;      

	// get db image list
	char tmpfullstr[FULLNAME_SZ];
	char tmpfullstr1[FULLNAME_SZ];

	vector<string> db_img_names;
	sprintf(tmpfullstr, "%s/%s", DBdir, DBfilelist);
	sprintf(tmpfullstr1, "%s/%s", Testdir, Testfilelist);
	readTextFile(tmpfullstr, db_img_names);

	// *** read image
	vector<Mat> db_img;
	getImageList(DBdir, db_img_names, db_img);

		
	// read & extract feature from db img list
	vector<KeyPoint> keypoints_obj;
	vector<uchar> vect_tmp;
	Mat descriptors_obj; 
	Mat img, org_img;
	for(int i=0; i<db_img.size(); i++)
	{
		//img = imread(imgfile0, 0);
		db_img[i].copyTo(org_img);
		cvtColor(org_img, img, COLOR_RGB2GRAY); 

		/*imshow("received image", img);
		waitKey(0);*/

		SIFT SIFTdect_obj = SIFT(sample_sz, 3, 0.04, 10, 1.6);

		SIFTdect_obj(img, noArray(), keypoints_obj, descriptors_obj, false);  // noArray(): using no mask

		//MatInfo(descriptors_obj);
		//MATmtxdisp(descriptors_obj);
		//getchar();

		DBKeypointAdd(keypoints_obj, db_keypoints);
		DBDescriptorAdd(descriptors_obj, db_descriptors);
		//db_keypoints.push_back(keypoints_obj);    // IMPOSSIBLE!!!
		//MatInfo(Mat(db_descriptors));
	}

	// convert to correct format
	Mat db_descriptors_cvtd = Mat::zeros(db_descriptors.size(), db_descriptors[0].cols, db_descriptors[0].depth());
	for (int i=0; i<db_descriptors.size(); i++)
	{
		for (int j=0; j<db_descriptors[0].cols; j++)
		{
			db_descriptors_cvtd.at<float>(i,j) = db_descriptors[i].at<float>(j);  // depth = 5 -> float
		}
	}
	db_descriptors_cvtd.copyTo(ret_db_descriptors);

	// get db image list
	vector<string> test_img_names;
	char fullfilename[FULLNAME_SZ];
	sprintf(fullfilename,"%s/%s", Testdir, Testfilelist);
	readTextFile(fullfilename, test_img_names);

	// *** read image
	//vector<Mat> test_db_img;
	getImageList(Testdir, test_img_names, test_db_img);


	return 1;
}